import type { SearchParams } from "./types"
import { getVideoData } from "@@/apis/video/index"
import { ref } from "vue"

// 视频表格相关逻辑
export function useTable() {
  const tableData = ref<any[]>([])
  // 获取默认搜索参数
  const getSearchParams = (): SearchParams => ({
    videoName: "",
    actor: "",
    series: "",
    category: "",
    videoType: "",
    resolution: "",
    subtitle: ""
  })

  const searchParams = ref<SearchParams>(getSearchParams())
  const currentPage = ref(1)
  const pageSize = ref(20)
  const total = ref(0)

  // 获取数据
  const fetchData = async () => {
    const params = {
      ...searchParams.value,
      page: currentPage.value,
      pageSize: pageSize.value
    }
    try {
      const res = await getVideoData(params) as { data: { list: any[], total: number } }
      tableData.value = res.data.list || []
      total.value = res.data.total || 0
    } catch (error) {
      tableData.value = []
      total.value = 0
      console.error("获取视频数据失败:", error)
    }
  }

  // 分页切换
  const handlePageChange = (page: number) => {
    currentPage.value = page
    fetchData()
  }

  // 每页数量切换
  const handleSizeChange = (size: number) => {
    pageSize.value = size
    fetchData()
  }

  return {
    tableData,
    searchParams,
    currentPage,
    pageSize,
    total,
    fetchData,
    handlePageChange,
    handleSizeChange,
    getSearchParams
  }
}
